<?php

namespace app\merchant\controller;

use app\common\model\SinceModel;
use think\Db;

class Since extends Common
{
    public function index(){
        $where =[
            'since_merchant_id' => session('merchant.merchant_id'),
            'since_is_delete' => '0',
        ];
        $page_number = 10;
        $data['since_list'] = SinceModel::since_sel($where, $page_number);
        $data['page'] = $data['since_list']->render();
        return view('since', ['data' => $data]);
    }

    //添加自提点页面
    public function add(){
        return view('addsince');
    }

    //添加自提点数据
    public function do_add(){
        $data['since_number'] = input('post.number');
        $data['since_name'] = input('post.name');
        $data['since_tel'] = input('post.tel');
        $data['since_business_time_desc'] = input('post.time1');
        //$data['since_business_end_hours'] = input('post.time2');
        $data['province_name'] = input('post.province');
        $data['city_name'] = input('post.city');
        $data['region_name'] = input('post.region');
        $data['since_detailed_address'] = input('post.address');
        $data['since_create_time'] = time();
        $data['since_merchant_id'] = session('merchant.merchant_id');
        $ret = Db::table('since')->insert($data);
        if(!$ret){
            return returnJson(0, '添加自提点失败');
        }
        return returnJson(1, '添加自提点成功');
    }

    //修改自提点页面
    public function edit(){
        $since_id = input('get.since_id');
        $data['since'] = SinceModel::get($since_id);
        if(!$data['since']){
            return returnJson('', 300, '该自提点不存在');
            //$this->error('该自提点不存在')
        }
        return view('editsince', ['data' => $data]);
    }

    //修改自提点数据
    public function do_edit(){
//        $validate = validate('since');
//        $validate->scene('upload');
//        if(!$validate->check(input('post.'))){
//            $error_msg = $validate->getError();
//            return returnJson('', 300, $error_msg);
//        }
        $since_id = input('post.since_id');
        $since = SinceModel::get($since_id);
        if(!$since){
            return returnJson(0, '该自提点不存在');
        }
//        if($since['since_merchant_id'] != session('merchant.merchant_id')){
//            return returnJson('', 300, '该自提点不属于你');
//        }
        $where =[
            'since_id' => $since_id,
        ];
        $data['since_number'] = input('post.number');
        $data['since_name'] = input('post.name');
        $data['since_tel'] = input('post.tel');
        $data['since_business_time_desc'] = input('post.time1');
        //$data['since_business_end_hours'] = input('post.time2');
        $data['province_name'] = input('post.province');
        $data['city_name'] = input('post.city');
        $data['region_name'] = input('post.region');
        $data['since_detailed_address'] = input('post.address');
        $data['since_update_time'] = time();
        $ret = Db::table('since')->where($where)->update($data);
        if(!$ret){
            return returnJson(0, '修改自提点失败');
        }
        return returnJson(1, '修改自提点成功');
    }

    //删除商户自提点
    public function del(){
        $since_id = input('get.since_id');
        $since = SinceModel::get($since_id);
        if(!$since){
            return returnJson('', 300, '该自提点不存在，请刷新页面后，重新删除');
        }
        if($since['since_merchant_id'] != session('merchant.merchant_id')){
            return returnJson('', 300, '该自提点不属于你');
        }
        $where = [
            'since_id' => input('since_id'),
        ];
        $ret = Db::table('since')->where($where)->update(['since_is_delete' => 1]);
        return redirect('Since/index');
    }
}
